package com.zhx.zaxiang.线程池.demo1;

import org.springframework.stereotype.Component;

import java.util.concurrent.*;

@Component
public class Demo {

    // 1. 创建一个线程池对象 核心线程为3, 最大线程数5, 临时线程存活6秒, 任务队列最大为5
    public static ExecutorService pool = new ThreadPoolExecutor(3, 5, 6,
            TimeUnit.SECONDS, new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {

        CountDownLatch latch=new CountDownLatch(10);


        for (int i = 0; i <10 ; i++) {
            int finalI = i;
            pool.execute(() -> {
                System.out.println(Thread.currentThread()+"=================i"+ finalI);
                //逻辑处理
                latch.countDown();
                try {
                    Thread.sleep(3000);
                    System.out.println(finalI);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        //等待所有线程执行完毕,返回主线程
        try {
            latch.await();
            System.out.println("结束!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
